package cn.fyl.user.service.impl;

import cn.fyl.basic.exception.BusinessException;
import cn.fyl.basic.service.impl.BaseServiceImpl;
import cn.fyl.basic.util.Md5Utils;
import cn.fyl.basic.util.StrUtils;
import cn.fyl.user.domain.LoginInfo;
import cn.fyl.user.domain.User;
import cn.fyl.user.dto.RegisterDto;
import cn.fyl.user.mapper.LoginInfoMapper;
import cn.fyl.user.mapper.UserMapper;
import cn.fyl.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;



@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public void phoneRegister(RegisterDto dto) {
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        String phone = dto.getPhone();
        String verifyCode = dto.getVerifyCode();
        // 校验
        // 1.非空校验
        if(StringUtils.isEmpty(phone)
           ||StringUtils.isEmpty(phone)
           ||StringUtils.isEmpty(phone)
           ||StringUtils.isEmpty(phone)){
            throw new BusinessException("参数不能为空");
        }
        // 2.检验两次密码是否一致
        if(!password.equals(passwordRepeat)){
            throw new BusinessException("两次输入密码不一致，请重新输入");
        }
        // 3.手机号验证，防老6
        User userPhone =  userMapper.findByPhone(phone);
        if(userPhone!=null){
            throw new BusinessException("该手机号已经被注册，请直接登陆");
        }
        // 4.校验验证码是否过期
        Object value = redisTemplate.opsForValue().get(phone + "phoneRegister");
        if(value==null){
            throw new BusinessException("验证码过期，请重新获取");
        }
        // 5.判断输入验证码是否正确
        String code = value.toString().split(":")[0];
        if(!code.equalsIgnoreCase(verifyCode)){
            throw new BusinessException("验证码输入错误，请重新输入");
        }
        // 保存数据到数据库，先保存logininfo
        LoginInfo loginInfo = getLoginInfo(dto);

        User user = loginInfo2User(loginInfo);
        // 保存到数据库，并返回自增主键
        loginInfoMapper.save(loginInfo);
        // 把返回的主键赋值给user，再保存
        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);


    }
    public User loginInfo2User(LoginInfo loginInfo){
        User user = new User();
        // copy相同的属性
        BeanUtils.copyProperties(loginInfo,user);
        return user;
    }

    public LoginInfo getLoginInfo(RegisterDto dto){
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setPhone(dto.getPhone());
        loginInfo.setUsername(dto.getPhone());
        loginInfo.setType(1);
        // 生成32位盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 将密码加盐加密
        String dbPassword = Md5Utils.encrypByMd5(salt + dto.getPassword());
        loginInfo.setSalt(salt);
        loginInfo.setPassword(dbPassword);


        return loginInfo;
    }
}
